package main

import (
	"./base_class"
	"errors"
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"log"
	"os"
)

const (
	ACCESS_ID     = "LTAI4GJxCEz1bHaduygZFg83"
	ACCESS_SECRET = "MGT8mZvdAxswM3JRwcFof7xzFmIOYb"
	ENDPOINT      = "oss-cn-shenzhen.aliyuncs.com"
)

var (
	client    *oss.Client
	createErr error
	fileSum   map[string]int
	fileSlice map[string]int
)

func main() {

}
func createClient() {
	client, createErr = oss.New(ENDPOINT, ACCESS_ID, ACCESS_SECRET)
	if createErr != nil {
		log.Println(createErr)
		os.Exit(-1)
	}
	log.Println("Connect success")
}
func isBucketExist(bucketName string) bool {
	isExist, err := client.IsBucketExist(bucketName) //bool
	if err != nil {
		handleError(err)
	}
	return isExist
}
func createBucket(bucketName string, acl oss.ACLType) {
	if bucketName == "access-log" || bucketName == "test-log" {
		handleError(errors.New("The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again "))
	}
	if !isBucketExist(bucketName) {
		err := client.CreateBucket(bucketName, oss.ACL(acl))
		if err != nil {
			handleError(err)
		}
	} else {
		handleError(errors.New("This bucket name already exists "))
	}
}
func getBucketList() {
	if client == nil {
		log.Println("没有创建连接")
		os.Exit(-1)
	}
	market := "create"
	IsRes, err := client.ListBuckets(oss.Marker(market))
	if err != nil {
		log.Println(err)
		os.Exit(-1)
	}
	for _, bucket := range IsRes.Buckets {
		fmt.Println("Bucket:", bucket.Name)
	}
}
func getBucketInfo(bucketName string) {
	if client == nil {
		log.Println("没有创建连接")
		os.Exit(-1)
	}
	res, err := client.GetBucketInfo(bucketName)
	if err != nil {
		log.Println(err)
		return
	}
	bucket := base_class.CreateBucketInfo(res)
	fmt.Println(bucket.ToString())
}
func upload() {
	bucketName := "create-test"
	objectName := "favicon.ico"
	localName := "myblog/favicon.ico"
	//获取存储空间
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Println(err)
		os.Exit(-1)
	}
	err = bucket.PutObjectFromFile(objectName, localName)
	if err != nil {
		log.Println(err)
		os.Exit(-1)
	}
}
func uploadSplit() {
	//分片上传
	bucketName := "create-test"
	objectName := "favicon.ico"
	localName := "myblog/favicon.ico"
	//获取存储空间
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Println(err)
		os.Exit(-1)
	}
	//分片
	chunks, err := oss.SplitFileByPartNum(localName, 100)
	if err != nil {
		log.Println(err)
		os.Exit(-1)
	}

	//打开文件
	fd, err := os.Open(localName)
	if err != nil {
		log.Println(err)
		os.Exit(-1)
	}
	defer fd.Close()

	// 指定存储类型为标准存储。
	storageType := oss.ObjectStorageClass(oss.StorageArchive)

	// 步骤1：初始化一个分片上传事件，并指定存储类型为标准存储。
	imur, err := bucket.InitiateMultipartUpload(objectName, storageType)
	// 步骤2：上传分片
	var parts []oss.UploadPart
	fmt.Print("上传进度：")
	for _, chunk := range chunks {
		_, _ = fd.Seek(chunk.Offset, os.SEEK_SET)
		part, err := bucket.UploadPart(imur, fd, chunk.Size, chunk.Number)
		if err != nil {
			log.Println(err)
			os.Exit(-1)
		}
		parts = append(parts, part)
		fmt.Print("■")
	}
	fmt.Println()

	// 指定Object的读写权限为公共读，默认为继承Bucket的读写权限。
	objectAcl := oss.ObjectACL(oss.ACLPublicRead)

	// 步骤3：完成分片上传，指定文件读写权限为公共读。
	fmt.Println("正在合并分片")
	cmur, err := bucket.CompleteMultipartUpload(imur, parts, objectAcl)
	if err != nil {
		fmt.Println("Error:", err)
		fmt.Println("正在取消")
		err = bucket.AbortMultipartUpload(imur)
		if err != nil {
			handleError(err)
		}

	}
	fmt.Println("文件上传完毕")
	fmt.Println("cmur:", cmur)
}
func getObjectList(bucketName string) []string {
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Println(err)
		os.Exit(-1)
	}
	marker := ""
	IsRes, err := bucket.ListObjects(oss.Marker(marker))
	if err != nil {
		log.Println(err)
		os.Exit(-1)
	}
	objNames := make([]string, 0, 100)
	for _, obj := range IsRes.Objects {
		objNames = append(objNames, obj.Key)
	}
	return objNames
}
func deleteObject(bucketName string) {
	bucket, _ := client.Bucket(bucketName)
	objNames := getObjectList(bucketName)
	defRes, err := bucket.DeleteObjects(objNames)
	if err != nil {
		log.Println(err)
		os.Exit(-1)
	}
	fmt.Println("Delete object:", defRes.DeletedObjects)
}
func handleError(err error) {
	log.Println(err)
	os.Exit(-1)
}
func getLogging() {
	logRes, err := client.GetBucketLogging("create-test")
	if err != nil {
		handleError(err)
	}
	fmt.Println("Target bucket ：", logRes.LoggingEnabled.TargetBucket)
	fmt.Println("Target prefix : ", logRes.LoggingEnabled.TargetPrefix)

}
func openLogging() {
	err := client.SetBucketLogging("create-test", "kumasanli-test-log", "kumasanli", true)
	if err != nil {
		handleError(err)
	}
	log.Println("日志开启成功")
}
